package com.yanqu.road.server.manager.config;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.servercenter.ChannelSystemConfig;
import com.yanqu.road.logic.bussiness.config.ConfigBussiness;
import com.yanqu.road.logic.redis.RedisKeyDefine;
import com.yanqu.road.logic.redis.RedisKeyDefineHelper;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.reids.subpub.RedisReliableMessageSubPub;
import com.yanqu.road.reids.subpub.eRedisChannelType;
import com.yanqu.road.server.manger.ChannelSystemConfigMgr;
import com.yanqu.road.server.redis.SystemConfigRedisSubPub;
import com.yanqu.road.utils.ConfigHelper;

import java.util.Map;

public class CrossChannelSystemConfigMgr extends ChannelSystemConfigMgr {

    @Override
    public boolean init() throws Exception {
        RedisReliableMessageSubPub.addSubPub(new SystemConfigRedisSubPub());
        return reloadConfig() && reloadData();
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadAndSetRedisConfigMap();
        notifyReload();
        return true;
    }

    public static void reloadAndSetRedisConfigMap() {
        getLogger().info("reloadAndSetRedisConfigMap  start");
        int channelId = ConfigHelper.getInt("channelId");
        Map<Integer, Map<String, ChannelSystemConfig>> systemConfigMap = ConfigBussiness.getChannelSystemConfigMap(channelId);
        setConfigMap(channelId, systemConfigMap);
        RedisManger.getRedisUtil().setString(RedisKeyDefineHelper.getChannelSystemConfigKey(channelId), JSON.toJSONString(systemConfigMap));
        Long count = RedisManger.getRedisUtil().publish(eRedisChannelType.SystemConfig.getChannel(), RedisKeyDefineHelper.getChannelSystemConfigPublishMsg(channelId));
//        getLogger().info("systemConfigMap:{}", JSON.toJSONString(systemConfigMap));
        getLogger().info("reloadAndSetRedisConfigMap  end");
    }

}
